class Pair {
    constructor(key, value) {
        this.key = key;
        this.value = value;
    }
}


class HashTable {
    constructor(size) {
        this.size = size;
        this.table = new Array(size).fill(null);
    }

    // 哈希函数
    hashFunc(key) {
      return key % this.size;
    }

    // 查询操作
    get(key) {
      let index = this.hashFunc(key);
      let pair = this.table[index];
      if (pair && pair.key === key) {
        return pair.value;
      } else {
        return undefined;
      }
    }

    // 插入操作
    set(key, value) {
      let index = this.hashFunc(key); 
      this.table[index] = new Pair(key, value);
    }

    // 删除操作
    delete(key) {
      let index = this.hashFunc(key);
      this.table[index] = null;
    }

    // 获取所有键值对
    entries() {
      let pairs = [];
      for (let i = 0; i < this.table.length; i++) {
        if (this.table[i]) {
          pairs.push(this.table[i]);
        }
      }
      return pairs;
    }

    // 获取所有键
    keys() {
      let keys = [];
      for (let i = 0; i < this.table.length; i++) {
        if (this.table[i]) {
          keys.push(this.table[i].key);
        }
      }
      return keys;
    }

    // 获取所有值
    values() {
      let values = [];
      for (let i = 0; i < this.table.length; i++) {
        if (this.table[i]) {
          values.push(this.table[i].value);
        }
      }
      return values;
    }

    // 打印哈希表

    print() {
      let pairs = this.entries();
      pairs.forEach(pair => {
        console.log(`key: ${pair.key}, value: ${pair.value}`);
      })
    }
}



// 测试
let hashTable = new HashTable(10);
hashTable.set(1, 'a');
hashTable.set(2, 'b');  
hashTable.set(3, 'c');  
hashTable.set(4, 'd');  
hashTable.set(5, 'e');  
hashTable.set(6, 'f');  
hashTable.set(7, 'g');  
hashTable.set(8, 'h');  
hashTable.set(9, 'i');  
hashTable.set(10, 'j');

console.log(hashTable.get(1)); // a
console.log(hashTable.get(2)); // b

hashTable.delete(1);
console.log(hashTable.get(1)); // undefined

hashTable.print(); // key: 2, value: b